package com.ifengchao.service.impl;

import com.ifengchao.dao.iface.DealReptileDao;
import com.ifengchao.pojo.HConfig;
import com.ifengchao.pojo.HCrawlLog;
import com.ifengchao.pojo.HMessagePlat;
import com.ifengchao.service.DealSpecialReptileService;
import com.ifengchao.util.ReptileUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * The type Deal special reptile service.
 *
 * @Author YiMing
 * @Description:
 * @Date: Created in 下午2:14 2017/9/13
 * @Modified By
 */
@Service
public class DealSpecialReptileServiceImpl implements DealSpecialReptileService {

	private static final Logger logger = Logger.getLogger(DealReptileServiceImpl.class);

	@Autowired(required = true)
	private DealReptileDao dealReptileDao;

	private static String hyzUrl = null; //慧驿站url

	@Override
	public void getData() {
		//查询爬虫配置开关是否开启
		Map<String, String> map = dealReptileDao.querySetting();
		String flag = map.get("reptile_thread_flag");

		hyzUrl = map.get("hyzUrl");

		logger.info("开关状态是:"+ flag);

		//用于存储爬虫运行时间list
		ArrayList<HCrawlLog> hCrawlLogs = new ArrayList<>();

		if ("Y".equals(flag)) {
			List<HConfig> hConfigList = dealReptileDao.queryConfig();
			for (HConfig hConfig : hConfigList) {
				if ("P00014".equals(hConfig.getPlatNo())) {
					ReptileUtil.getMessage(hCrawlLogs,hConfig,"糗事百科", dealReptileDao);
				}
			}
			logger.info("开始插入爬虫记录表");
			dealReptileDao.insertCrawlLog(hCrawlLogs);
		} else {
			logger.info("=========爬虫配置开关没有开启========");
		}
	}


	/**
	 * Gets qiu shi bai ke data.
	 * 糗事百科
	 * @param dealReptileDao the deal reptile dao
	 * @param hConfig        the h config
	 * @throws Exception the exception
	 */
	public static void getQiuShiBaiKeData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {

		String site = hConfig.getSite(); //首页网址
		String secondSite = hConfig.getSecondSite(); //二级网页网址
		String platNo = hConfig.getPlatNo(); //资讯编号

		//根据platNo去资讯表中查询来源
		String mediaSource = dealReptileDao.queryMenu(platNo); //数据来源

		if (mediaSource == null) {
			logger.info("===========没有查询到数据来源==========");
			return;
		}

		String title = null; //标题
		String picture = null; //配图
		String content = null; //内容
		String column = null; //栏目
		String author = null; //作者

		List<HMessagePlat> hMessagePlats = new ArrayList<>();

		Integer index = 1;

		while (true) {
			//当条数等于50条执行插入
			if (hMessagePlats.size() >=30) {
				dealReptileDao.insertSpecialMessage(hMessagePlats);
				return;
			}
			//糗事百科
			String homePage = ReptileUtil.getHtmlData(site + index + "/"); //获取首页html信息
			Document parse = Jsoup.parse(homePage);

			Elements select2 = parse.select("div.article");
			//遍历 获取每篇文章内容详情
			//outer:
			for (Element element : select2) {
				picture = null;
				author = element.select("div.author a").text();
				String href = element.select("a.contentHerf").get(0).attr("href");

				//获取文章内容
				String htmlData = ReptileUtil.getHtmlData(secondSite + href);

				if (htmlData == null || "".equals(htmlData)) {
					logger.info("未知错误!!!!请求地址是:"+secondSite+href);
					if (hMessagePlats.size() > 0) {
						dealReptileDao.insertSpecialMessage(hMessagePlats);
					}
					return;
				}

				//获取文章title和内容
				Document parse1 = Jsoup.parse(htmlData);

				Elements imgs = parse1.select("div.thumb img");
				if (imgs != null && imgs.size() > 0) {
					picture = imgs.get(0).attr("src");
					if (picture.startsWith("//")) {
						picture = picture.substring(2);
						//上传图片到FastDFS
						picture = ReptileUtil.upload(picture);
						if (picture == null) {
							continue;
						}
					}
				}

				title = parse1.select("title").text();

				Elements select = parse1.select("div.content");
				String s1 = select.toString();

				String all = s1.replaceAll("<br>", "br标签<br>");
				content = Jsoup.parse(all).text().replaceAll("br标签", "<br><br>");


				//获取关键字
				String keyword = hConfig.getKeyword();
				//判断关键字是否为null 为null默认全爬取,不为null的话获取带有关键字的title文章
				if (keyword != null && !"".equals(keyword.trim())) {
					if (!title.contains(keyword)) {
						continue;
					}
				}
				//将文章内容中的%替换成全角的%
				if (content != null && content.indexOf("%") != -1) {
					content = content.replaceAll("%","％");
				}

				//获取栏目
				column = parse1.select("a.source-column").text();
				//糗事百科因为图片只有一张所以不用再查.如果有2张以上在放开此代码
				//Elements Epicture = parse1.select("div.thumb img");

				if ("".equals(author)) {
					author = "匿名用户";
				}

				/*if (Epicture != null && Epicture.size() > 0) {
					picture = Epicture.get(0).attr("src");
					picture = upload(picture);
				}*/

				HMessagePlat hMessagePlat = new HMessagePlat();

				hMessagePlat.setAuthor(author);

				hMessagePlat.setMediaSource(mediaSource);
				if (column == null) {
					column = "糗事百科";
				}
				hMessagePlat.setPublishSub(column);

				hMessagePlat.setMessageImg(picture);

				if (picture != null) {
					hMessagePlat.setMessageContent(content + " " + "<img src='" + hyzUrl + picture + "'/>");
				} else {
					hMessagePlat.setMessageContent(content);
				}

				//将内容中的%替换成全角的%
				if (title != null && title.indexOf("%") != -1) {
					title = title.replaceAll("%", "％");
				}

				title = title.replace("- 糗事百科", "");

				hMessagePlat.setMediaSourceUrl(secondSite + href);
				hMessagePlat.setMessageTitle(title);
				hMessagePlat.setPlatNo(platNo);

				hMessagePlats.add(hMessagePlat);
			}
			index++;
		}
	}
}
